package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 注册用户资源控制器
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public Object register(User user){
        JSONResult result = new JSONResult();
        try {
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping("/{id}")
    public Object update(User user){
        JSONResult result = new JSONResult();
        result.setResult(user);
        userService.saveOrUpdate(user);
        return result;
    }

    @GetMapping("/{authorId}/travels")
    public Object queryTravels(TravelQuery qo){
        qo.setOrderBy("lastUpdateTime desc");
        return travelService.query(qo);
    }

    /**
     * 通过id查询用户信息
     * @param id id值
     * @return
     */
    @GetMapping("/{id}")
    public Object selectById(@PathVariable Long id){
        return userService.selectById(id);
    }
}
